package 优先算法.滑动窗口.最大连续1的个数3;

public class teacher {
    public static void main(String[] args) {
        Solution2 so2 = new Solution2();
        int[] nums = {1,1,1,0,0,0,1,1,1,1,0};
        int[] nums2 = {0,0,0,1};

        int rel = so2.longestOnes(nums, 2);
        int rel2 = so2.longestOnes(nums2, 4);
        System.out.println(rel);
        System.out.println(rel2);
    }
}
class Solution2 {
    public int longestOnes(int[] nums, int k) {
        int zero=0,len=0,left=0,right=0,n=nums.length;
        for(;right<n;right++){//进窗口
            if(nums[right] == 0){
                zero++;
            }
                while(zero > k){//出窗口
                    if(nums[left++]==0){
                        zero--;
                    }
                }
                //更新
                len=Math.max(len,right-left+1);

        }
        return len;
    }
}